package com.mycjj.android.activity;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.ViolationBaseBean;
import com.mycjj.android.bean.ViolationBaseBean.Violation;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class WeiZhangActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_weizhang);
		init();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("违章");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				WeiZhangActivity.this.finish();
			}
		});
	}

	private ListView lv_wz;
	private WeizhangAdapter wzAdapter;
	private TextView tv_search;
	private TextView tv_name;
	private void init() {
		initHeader();
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_search = (TextView) findViewById(R.id.tv_search);
		tv_search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.e("mark", "tv_search onClick");
				initData();
			}
		});
		lv_wz = (ListView) findViewById(R.id.lv_wz);
		wzAdapter = new WeizhangAdapter(this);
		lv_wz.setAdapter(wzAdapter);

		initData();
	}

	private void initData() {
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		String default_car_id = Preferences.getSharedPreferences(this, "default_car_id", "");
		Log.e("mark", "default_car_id:"+default_car_id);
		params.put("car_id", default_car_id);
		request.requestUserCarGetViolationWithDoneHandler("", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				ViolationBaseBean baseBean = dataManager.getViolationBaseBean();
				int count = 0;
				if (baseBean != null) {
					if (baseBean.getCode() == 200) {
						List<Violation> list = baseBean.getData();
						if(list != null && list.size() > 0){
							count = list.size();
						}
						wzAdapter.setList(list);
					} else {
						Toast.makeText(WeiZhangActivity.this,  baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				} else {
					Log.e("mark", "ViolationBaseBean is null!");
				}
				tv_name.setText("您有"+count+"条违章记录");
			}
			
		});
	}

	public class WeizhangAdapter extends TAdapter<Violation> {

		public WeizhangAdapter(Context mContext) {
			super(mContext);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_weizhang_view, null);
				holder = new ViewHolder();
				holder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
				holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
				holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
				holder.tv_score = (TextView) convertView.findViewById(R.id.tv_score);
				holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
				holder.tv_state = (TextView) convertView.findViewById(R.id.tv_state);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Violation bean = mList.get(position);
			if (bean != null) {
				holder.tv_address.setText(bean.getArea());
				holder.tv_desc.setText(bean.getAct());
				holder.tv_date.setText(bean.getDate());
				holder.tv_score.setText("扣分:" + bean.getFen() + "分");
				holder.tv_money.setText("罚款:" + bean.getMoney() + "元");

				// 是否处理,1处理 0未处理 空未知
				String state = "";
				if (TextUtils.isEmpty(bean.getHandled())) {
					state = "未知";
					holder.tv_state.setBackgroundColor(Color.parseColor("#999999"));
				} else if ("0".equals(bean.getHandled())) {
					state = "未处理";
					holder.tv_state.setBackgroundColor(Color.parseColor("#e83710"));
				} else if ("1".equals(bean.getHandled())) {
					state = "已处理";
					holder.tv_state.setBackgroundColor(Color.parseColor("#1f9a25"));
				} else {
					state = "未知";
					holder.tv_state.setBackgroundColor(Color.parseColor("#999999"));
				}

				holder.tv_state.setText(state);
			}
			return convertView;
		}

		class ViewHolder {
			TextView tv_address;
			TextView tv_desc;
			TextView tv_date;
			TextView tv_score;
			TextView tv_money;
			TextView tv_state;
		}

	}

}
